package com.ruoyi.ora.netty.channel;

import com.ruoyi.common.utils.ByteUtil;
import com.ruoyi.ora.netty.channel.base.TcpDecoderBaseHandler;
import com.ruoyi.ora.netty.channel.base.TcpEncoderBaseHandler;
import com.ruoyi.ora.netty.channel.handler.ForwardAudioHandler;
import com.ruoyi.ora.netty.channel.stick.HeadLengthTailFieldHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

public class ChannelTcpInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception{
        ChannelPipeline ph = socketChannel.pipeline();

        //心跳判断
        ph.addLast(new IdleStateHandler(60000, 0, 0, TimeUnit.MILLISECONDS));

        ph.addLast(new TcpDecoderBaseHandler());
        ph.addLast(new TcpEncoderBaseHandler());

        ph.addLast(new HeadLengthTailFieldHandler(ByteUtil.hexToBytes("EB90"),
                null, 18, 2, 0, 30));

        ph.addLast(new ForwardAudioHandler());
    }
}
